//
//  NormalTableView.m
//  MacDevTest
//
//  Created by 夏利兵 on 2020/8/12.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "NormalTableView.h"
#import "CustomRowView.h"

@interface NormalTableView ()<NSTabViewDelegate,NSTableViewDataSource>
@property (nonatomic,strong)  NSTableView *tableView;

@property (nonatomic, strong) NSMutableArray<NSArray*> *listDatas;
@end

@implementation NormalTableView

- (void)windowDidLoad {
    [super windowDidLoad];
    
    NSArray* array1 = @[@"col1_field1",@"col1_field2"];
    NSArray* array2 = @[@"col2_field1",@"col2_field2"];

    self.listDatas =  [NSMutableArray arrayWithObjects:array1,array2, nil];
    
    NSScrollView * scrollView    = [[NSScrollView alloc] init];
    scrollView.hasVerticalScroller  = YES;
    scrollView.frame = self.window.contentView.bounds;
    [self.window.contentView addSubview:scrollView];
    self.tableView = [[NSTableView alloc]initWithFrame:self.window.contentView.bounds];
    NSTableColumn * column = [[NSTableColumn alloc]initWithIdentifier:@"col1"];
    column.title = @"col1";
    [_tableView addTableColumn:column];
    
    
//    NSTableColumn * column2 = [[NSTableColumn alloc]initWithIdentifier:@"col2"];
//    column2.title = @"col2";
//    [_tableView addTableColumn:column2];
    
    scrollView.contentView.documentView = _tableView;

    //Assigning to 'id<NSTableViewDelegate> _Nullable' from incompatible type 'NormalTableView *const __strong'
    _tableView.delegate = (id)self;
    _tableView.dataSource = self;
    [_tableView reloadData];
    
    [_tableView reloadData];
}

#pragma mark- delegate

- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    return YES;
}

- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    NSLog(@"willSelectTabViewItem");
}

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    NSLog(@"didSelectTabViewItem :%@",tabViewItem.identifier);
}

- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView
{
    NSLog(@"tabViewDidChangeNumberOfTabViewItems");
}

#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.listDatas.count;
}

// 设置行高
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{
    return 40;
}


//cell-based(基于单元格) 模式 cell直接填内容
//- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
//{
//
//    NSArray *array = [_listDatas objectAtIndex:row];
//
//    if ([tableColumn.identifier isEqualTo:@"col1"]) {
//        return array[0];
//    }
//
//    return array[1];
//}


- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
    //修改选中颜色
    CustomRowView *rowView = [[CustomRowView alloc] init];
    return rowView;
}

//view-based模式(基于视图)

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    // 1.0.创建一个Cell
//       NSTextField *view   = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 30)];
//       view.bordered       = NO;
//       view.editable       = NO;
//
//       // 1.1.判断是哪一列
//       if ([tableColumn.identifier isEqualToString:@"columen1"]) {
//           view.stringValue    = [NSString stringWithFormat:@"第1列的第%ld个Cell",row + 1];
//       }else if ([tableColumn.identifier isEqualToString:@"columen2"]) {
//           view.stringValue    = [NSString stringWithFormat:@"第2列的第%ld个Cell",row + 1];
//       }else {
//           view.stringValue    = [NSString stringWithFormat:@"不知道哪列的第%ld个Cell",row + 1];
//       }
//       return view;
    
//    NSTableRowView *cellView = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
//    if (!cellView) {
//        cellView = [[NSTableRowView alloc]initWithFrame:NSMakeRect(0, 0, 300, 40)];
//    }
//    cellView.layer.borderColor = [NSColor blueColor].CGColor;
////    cellView.imageView.image = [NSImage imageNamed:NSImageNameFolder]; //设置图片
////    cellView.textField.stringValue = @"11";        //设置文字
//    return cellView;
    
    NSView *view = [[NSView alloc]initWithFrame:NSMakeRect(0, 0, 300, 40)];
    view.wantsLayer = YES;
    view.layer.backgroundColor = [NSColor blackColor].CGColor;
    return view;
}



//- (nullable NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
//{
//    NSCell *cell = [[NSCell alloc]initTextCell:@"111"];
//
//    return cell;
//}

@end
